package com.chain.qs.modules.biz.service;

import com.chain.qs.common.service.CrudService;
import com.chain.qs.modules.biz.dao.StationGroupDao;
import com.chain.qs.modules.biz.entity.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 * 设备Service
 *
 * @author wangzhiping
 */
@Service
//@Transactional(readOnly = true)
public class StationGroupService extends CrudService<StationGroupDao, StationGroup> {
    @Resource
    private StationGroupCfgExtendService stationGroupCfgExtendService;
    @Resource
    private EquipmentItemRecordSensorService equipmentItemRecordSensorService;
    @Resource
    private EquipmentItemWarningRuleService equipmentItemWarningRuleService;
    @Resource
    private EquSwitcherService equSwitcherService;
    @Resource
    private WarningRecordService warningRecordService;

    @Transactional(readOnly = false)
    public void updateIntegrityYes(String id) {
        dao.updateIntegrityYes(id);
    }
    @Transactional(readOnly = false)
    public void updateIntegrityNo(String id) {
        dao.updateIntegrityNo(id);
    }

    public List<StationGroup> findListByOfficeId(String officeId) {
        return dao.findListByOfficeId(officeId);
    }

    public int isBaseGroupExist(String stationId) {
        return dao.isBaseGroupExist(stationId);
    }

    List<StationGroup> findListByGroupId(String groupId) {
        return dao.findListByGroupId(groupId);
    }

    @Transactional(readOnly = false)
    public void delete(StationGroup stationGroup) {
        for (StationGroupCfgExtend stationGroupCfgExtend : stationGroupCfgExtendService.equlistByGroup(stationGroup.getId())) {
            stationGroupCfgExtendService.delete(stationGroupCfgExtend);
        }
        for (WarningRecord warningRecord : warningRecordService.findList(new WarningRecord(stationGroup.getId(), null, null, null))) {
            warningRecordService.delete(warningRecord);
        }
        for (EquipmentItemWarningRule equipmentItemWarningRule : equipmentItemWarningRuleService.findList(new EquipmentItemWarningRule(stationGroup.getId(), null))) {
            equipmentItemWarningRuleService.delete(equipmentItemWarningRule);
        }
        for (EquSwitcher equSwitcher : equSwitcherService.findList(new EquSwitcher(stationGroup.getId(), null))) {
            equSwitcherService.delete(equSwitcher);
        }
//        deleteRecords(stationGroup.getId(), null);
        super.delete(stationGroup);
    }

    /**
     * 删除设备记录表中数据
     *
     * @param sgrpId   站组ID
     * @param deviceId 设备ID
     */
    @Transactional(readOnly = false)
    void deleteRecords(String sgrpId, String deviceId) {
        for (EquipmentItemRecordSensor equipmentItemRecordSensor : equipmentItemRecordSensorService.findList(new EquipmentItemRecordSensor(sgrpId, deviceId))) {
            equipmentItemRecordSensorService.delete(equipmentItemRecordSensor);
        }
    }
}
